@extends('layouts._app')
@section('content')
    <link rel="stylesheet" type="text/css" href="{{asset('static/admin/lib/select/formSelects-v4.css')}}"/>
    <style>
        #markdown-editor {
            z-index: 999999;
        }
    </style>
    <body>
    <div class="wrap-container">
        @include('layouts._flash')
        <input type="hidden">
        <div id="LAY-popup-user-add" class="layui-layer-content">
            <div class="layui-form" lay-filter="layuiadmin-form-label" style="padding: 20px 30px 0 0;">
                {!! Form::open(['route' => ['blog.article.update',$article->id],'class'=>'layui-form','style'=>'width: 90%;padding-top: 20px;','method'=>'put']) !!}
                {!! Form::hidden('filter','true') !!}
                <div class="layui-form-item">
                    {!! Form::label('','* 标题：',['class'=>'layui-form-label','style'=>'color:red']) !!}
                    <div class="layui-input-block">
                        {!! Form::text('title',"{$article->title}",['required','lay-verify'=>'required','class'=>'layui-input','placeholder'=>'请输入标题']) !!}
                    </div>
                </div>
                <div class="layui-form-item layui-form-text">
                    {!! Form::label('','分类：',['class'=>'layui-form-label']) !!}
                    <div class="layui-input-inline">
                        <select name="category_id" lay-search lay-filter="category" value>
                            <option value="">请选择</option>
                            <optgroup label="文章分类">
                                @foreach($category as $key => $value)
                                    @if($value['static'] == 1)
                                        <option value="{{$value['id']}}"
                                                @if($value['id'] == $article->category_id) selected
                                            @endif>{{$value['name']}}
                                        </option>
                                    @endif
                                @endforeach
                            </optgroup>
                            <optgroup label="问题分类">
                                @foreach($category as $key => $value)
                                    @if($value['static'] == 3)
                                        <option value="{{$value['id']}}"
                                                @if($value['id'] == $article->category_id) selected
                                            @endif>{{$value['name']}}
                                        </option>
                                    @endif
                                @endforeach
                            </optgroup>
                        </select>
                    </div>
                    <div class="layui-input-inline" style="width: 120px">
                        <select name="parent_id" lay-search lay-filter="parent">
                            <option value="">请选择</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item layui-form-text">
                    {!! Form::label('','标签：',['class'=>'layui-form-label']) !!}
                    <div class="layui-input-inline">
                        <select name="tag" lay-search multiple xm-select="select4">
                            <option value="">请选择</option>
                            @foreach($label as $key => $value)
                                <option value="{{$value['id']}}"
                                        @if($article->label) @if(in_array($value['id'],$article->label)) selected="selected" @endif @endif
                                >{{$value['name']}}
                                </option>
                            @endforeach
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    {!! Form::label('','封面图：',['class'=>'layui-form-label']) !!}
                    <div class="layui-input-block">
                        <img title="封面图" alt="封面图" id="img_show"
                             style="@if(!$article->file) display: none;@endif margin:0 20px 0 0;width: 60px;height: 60px;border-radius: 50%;"
                             src="@if($article->file) {{assetImage($article->file->url)}} @else {{asset('static/admin/icon/35508.png')}} @endif">
                        <button style="@if(!$article->file) display:none; @endif float: right;" type="button"
                                class="layui-btn layui-btn-small layui-btn-danger delBtn">
                            <i class="layui-icon">&#xe640;</i>
                        </button>
                        <div class="layui-box layui-upload-button">
                            {!! Form::file('',['id'=>'file0','onchange'=>'previewImages(this,0)']) !!}
                            <span class="layui-upload-icon"><i class="layui-icon"></i>上传图片</span>
                        </div>
                    </div>
                    {!! Form::hidden('pic','',['id'=>'pic']) !!}
                </div>
                <br>
                <div class="layui-form-item">
                    {!! Form::label('','是否显示：',['class'=>'layui-form-label']) !!}
                    <div class="layui-input-block">
                        <input type="radio" name="is_show" value="1" title="是"
                               @if($article->is_show == 1) checked @endif>
                        <input type="radio" name="is_show" value="2" title="否"
                               @if($article->is_show == 2) checked @endif>
                    </div>
                </div>
                <div class="layui-form-item">
                    {!! Form::label('','是否置顶：',['class'=>'layui-form-label']) !!}
                    <div class="layui-input-block">
                        <input type="radio" name="root" value="2" title="是" @if($article->root == 2) checked @endif>
                        <input type="radio" name="root" value="1" title="否" @if($article->root == 1) checked @endif>
                    </div>
                </div>
                <div class="layui-form-item">
                    {!! Form::label('','是否允许评论：',['class'=>'layui-form-label']) !!}
                    <div class="layui-input-block">
                        <input type="radio" name="is" value="1" title="允许" @if($article->is == 1) checked @endif>
                        <input type="radio" name="is" value="2" title="不允许" @if($article->is == 2) checked @endif>
                    </div>
                </div>
                <div class="layui-form-item">
                    {!! Form::label('','简介：',['class'=>'layui-form-label']) !!}
                    <div class="layui-input-block">
                        {!! Form::textarea('intro',"{$article->content->intro}",['class'=>'layui-textarea']) !!}
                    </div>
                </div>
                <div class="layui-form-item">
                    {!! Form::label('','内容：',['class'=>'layui-form-label']) !!}
                    <div class="layui-input-block">
                        @if($user->info->editor == 1)
                            <div id="markdown-editor">
                                <textarea style="display:none;"
                                          name="markdown">{{$article->content->markdown}}</textarea>
                            </div>
                            <link rel="stylesheet" type="text/css"
                                  href="{{asset('static/markdown/css/editormd.css')}}"/>
                            <script type="text/javascript" src="{{asset('static/markdown/editormd.js')}}"
                                    charset="utf-8"></script>
                            <script>
                                var testEditor = editormd("markdown-editor", {
                                    width: '100%',
                                    height: 600,
                                    previewTheme: 'default',
                                    editorTheme: 'pastel-on-dark',
                                    lineWrapping: true, // 编辑框不换行
                                    codeFold: true, // 代码折叠
                                    placeholder: '请输入...',
                                    syncScrolling: true,
                                    saveHTMLToTextarea: true, // 保存 HTML 到 Textarea
                                    searchReplace: true,
                                    watch: true, // 实时预览
                                    toolbar: true, // 工具栏
                                    previewCodeHighlight: true, // 预览 HTML 的代码块高亮，默认开启
                                    emoji: true,
                                    taskList: true,
                                    tocm: true, // Using [TOCM]
                                    tex: true, // 开启科学公式TeX语言支持，默认关闭
                                    flowChart: true, // 开启流程图支持，默认关闭
                                    sequenceDiagram: true, // 开启时序/序列图支持，默认关闭,
                                    imageUpload: true,
                                    imageFormats: ['jpg', 'jpeg', 'gif', 'png', 'bmp', 'webp'],
                                    imageUploadURL: '{{config('app.url')}}' + '/upload?user=blog&category=article&get=editormd-image-file&filter=true&post=markdown',
                                    path: "../../../static/markdown/lib/",   //你的path路径（原资源文件中lib包在我们项目中所放的位置）
                                    theme: "dark",//工具栏主题
                                });
                            </script>
                        @else
                            <textarea name="html" id="UEditor">{{$article->content->html}}</textarea>
                            <script type="text/javascript" charset="utf-8"
                                    src="{{asset('static/ueditor/ueditor.config.js')}}"></script>
                            <script type="text/javascript" charset="utf-8"
                                    src="{{asset('static/ueditor/ueditor.all.min.js')}}"></script>
                            <script>
                                UE.getEditor('UEditor');
                            </script>
                        @endif
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        {!! Form::submit('确认',['class'=>'layui-btn','lay-filter'=>'formDemo','lay-submit']) !!}
                    </div>
                </div>
                {!! Form::close() !!}
            </div>
        </div>
    </div>
    <script src="{{asset('static/admin/layui/layui.js')}}" type="text/javascript" charset="utf-8"></script>
    <script src="{{asset('js/jquery.min.js')}}" type="text/javascript" charset="utf-8"></script>
    <script type="text/javascript" src="{{asset('static/admin/lib/select/formSelects-v4.js')}}"
            charset="utf-8"></script>
    <script type="text/javascript" src="{{asset('js/upload.js')}}"></script>
    <script>
        //Demo
        layui.use(['form', 'jquery'], function () {
            var form = layui.form();
            $ = layui.jquery;
            form.render();
            $('.delBtn').click(function () {
                $(this).attr('style', 'display:none');
                $("#pic").val('');
                var img = $("#img_show");
                img.attr('src', '{{asset('static/admin/icon/35508.png')}}');
                img.attr('style', 'display:none');
            })

                @if($article->category_id)
            var category = '{{$article->category_id}}';
            var parent = '{{$article->parent_id}}';
            $.ajax({
                type: 'GET',
                url: "/blog/article/create",
                data: {parent_id: category},
                dataType: "json",
                complete: function (XHR) {
                    XHR = null;
                },
                error: function (xhr) {//获取ajax的错误信息
                    layer.alert(xhr.responseText, "Failed", function () {
                        layer.closeAll();
                    });
                },
                success: function (response) {
                    if (response.code == 403) {
                        layer.msg(response.msg, {icon: 4, time: 3000})
                        return false;
                    }
                    if (response.code != 200) {
                        layer.msg(response.msg, {icon: 5, time: 3000});
                        return false;
                    }
                    var pro = response.data;
                    var proHtml = '<option value="">请选择</option>';
                    for (var i = 0; i < pro.length; i++) {
                        if (parent == pro[i].id)
                            proHtml += '<option value="' + pro[i].id + '" selected>' + pro[i].name + '</option>';
                        else proHtml += '<option value="' + pro[i].id + '" >' + pro[i].name + '</option>';
                    }
                    //初始化省数据
                    $("select[name=parent_id]").html(proHtml).removeAttr("disabled");
                    form.render();
                }
            });
            @endif
            form.on('select(category)', function (proData) {
                $.ajax({
                    type: 'GET',
                    url: "/blog/article/create",
                    data: {parent_id: proData.value},
                    dataType: "json",
                    complete: function (XHR) {
                        XHR = null;
                    },
                    error: function (xhr) {//获取ajax的错误信息
                        layer.alert(xhr.responseText, "Failed", function () {
                            layer.closeAll();
                        });
                    },
                    success: function (response) {
                        if (response.code == 403) {
                            layer.msg(response.msg, {icon: 4, time: 3000})
                            return false;
                        }
                        if (response.code != 200) {
                            layer.msg(response.msg, {icon: 5, time: 3000});
                            return false;
                        }
                        var pro = response.data;
                        var proHtml = '<option value="">请选择</option>';
                        for (var i = 0; i < pro.length; i++) {
                            proHtml += '<option value="' + pro[i].id + '">' + pro[i].name + '</option>';
                        }
                        //初始化省数据
                        $("select[name=parent_id]").html(proHtml).removeAttr("disabled");
                        form.render();
                    }
                });
            });
        })
    </script>
    </body>
@endsection
